package edu.gmu.atelier;

/**
 * Utility matrix operations wrt double arrays.
 * @author James H. Pope
 */
public class DoubleUtil
{
    public static double[][] identityData(int n)
    {
        double[][] identity = new double[n][n];
        for (int i = 0; i < identity.length; i++)
        {
            identity[i][i] = 1.0;
        }
        return identity;
    }
    
    public static void mulAdd(double[][] a, int fromRow, double multiplier, int toRow)
    {
        for (int j = 0; j < a[0].length; j++)
        {
            a[toRow][j] += a[fromRow][j]*multiplier;
        }
    }
    
    public static void mulAdd(int startCol, double[][] a, int fromRow, double multiplier, int toRow)
    {
        for (int j = startCol; j < a[0].length; j++)
        {
            a[toRow][j] += a[fromRow][j]*multiplier;
        }
    }
    
    public static void swap(double[][] a, int row1, int row2)
    {
        for (int j = 0; j < a[0].length; j++)
        {
            double vc = a[row1][j];
            a[row1][j] = a[row2][j];
            a[row2][j] = vc;
        }
    }
    
    public static boolean equals(double[][] a, double[][] b)
    {
        int aRows = a.length;
        int aCols = a[0].length;
        int bRows = b.length;
        int bCols = b[0].length;
        
        if( aRows != bRows || aCols != bCols )
        {
            return false;
        }
        
        for (int i = 0; i < aRows; i++)
        {
            for (int j = 0; j < aCols; j++)
            {
                // Need epsilon check here
                if( a[i][j] != b[i][j] )
                {
                    return false;
                }
            }
        }
        return true;
    }
}
